package com.framework.view.indicator;

import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.widget.ViewPager2;

import com.framework.view.indicator.configs.BadgeConfig;
import com.framework.view.indicator.configs.TextTitleConfig;
import com.framework.view.indicator.indicators.configs.AbstractIndicatorConfig;
import com.framework.view.indicator.listener.OnBadgeChangeListener;

import java.util.ArrayList;
import java.util.List;

/**
 * 内置适配器
 *
 * @author liangzx
 * @version 1.0
 * @time 2019-12-17 12:01
 **/
public abstract class GodIndicatorAdapter<K extends AbstractIndicatorConfig> extends BaseIndicatorAdapter<K> {

    /**
     * 角标通知
     */
    private List<OnBadgeChangeListener> mBadgeChangeListeners;

    private List<String> mTabNames;

    public GodIndicatorAdapter() {
    }

    public GodIndicatorAdapter(ViewPager viewPager) {
        super(viewPager);
    }

    public GodIndicatorAdapter(ViewPager viewPager, List<String> tabNames) {
        super(viewPager);
        this.mTabNames = tabNames;
    }

    public GodIndicatorAdapter(ViewPager2 viewPager) {
        super(viewPager);
    }

    public GodIndicatorAdapter(ViewPager2 viewPager, List<String> tabNames) {
        super(viewPager);
        this.mTabNames = tabNames;
    }

    /**
     * 绑定标题配置
     *
     * @return
     */
    protected abstract TextTitleConfig bindTitleConfig();

    /**
     * 获取标题名称
     *
     * @param position
     * @return
     */
    public String getTitleName(int position) {
        if (null != mTabNames && mTabNames.size() > position) {
            return mTabNames.get(position);
        }
        if (null != mViewPager && mViewPager instanceof ViewPager) {
            PagerAdapter adapter = ((ViewPager) mViewPager).getAdapter();
            return null == adapter ? null : String.valueOf(adapter.getPageTitle(position));
        }
        return null;
    }

    @Override
    public int getCount() {
        if (null != mTabNames) {
            return mTabNames.size();
        } else {
            return super.getCount();
        }
    }

    /**
     * 获取标题配置
     *
     * @return
     */
    final TextTitleConfig getTitleConfig() {
        return bindTitleConfig();
    }

    /**
     * 获取角标文字内容
     *
     * @param position
     * @return
     */
    public int getBadgeNumber(int position) {
        return 0;
    }

    /**
     * 绑定角标配置
     *
     * @return
     */
    protected BadgeConfig bindBadgeConfig(int position) {
        return null;
    }

    /**
     * 添加角标变化通知
     *
     * @param listener
     */
    final void addBadgeChangeListener(OnBadgeChangeListener listener) {
        if (null == mBadgeChangeListeners) {
            mBadgeChangeListeners = new ArrayList<>(getCount());
        }
        mBadgeChangeListeners.add(listener);
    }

    /**
     * 移除所有角标变化通知
     */
    final void removeBadgeChangeListeners() {
        if (null != mBadgeChangeListeners) {
            mBadgeChangeListeners.clear();
        }
        mBadgeChangeListeners = null;
    }

    /**
     * 通知更新所有角标
     */
    public void notifyBadgeDataSetChanged() {
        if (null != mBadgeChangeListeners) {
            int index = 0;
            for (OnBadgeChangeListener listener : mBadgeChangeListeners) {
                listener.onBadgeChange(getBadgeNumber(index));
                index++;
            }
        }
    }

    /**
     * 通知更新单个角标
     *
     * @param position    位置
     * @param badgeNumber 数量
     */
    public void notifyBadgeDataSetChanged(int position, int badgeNumber) {
        if (null != mBadgeChangeListeners && mBadgeChangeListeners.size() > position) {
            OnBadgeChangeListener listener = mBadgeChangeListeners.get(position);
            listener.onBadgeChange(badgeNumber);
        }
    }

}
